package BS.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;

import BS.dao.caozuoyuanDao;
import BS.dao.daofactory;
import BS.dao.guanliyuanDao;
import BS.entity.caozuoyuan;
import BS.entity.guanliyuan;
import BS.utils.md5;

@WebServlet("/login")
public class LoginServlet extends HttpServlet{
		@SuppressWarnings("unused")
		@Override
		protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			
			String userName = req.getParameter("userName");
			String password = req.getParameter("password");
			String type = req.getParameter("type");
			
			if(StringUtils.isBlank(userName) || StringUtils.isBlank(password) || StringUtils.isBlank(type)) {
				req.setAttribute("error", "信息不能为空，请检查是否有空！");
				req.getRequestDispatcher("login.jsp").forward(req, resp);
				return;
			}
			
			HttpSession session = req.getSession();
			if(StringUtils.isNotBlank(type)) {
				if("0".equals(type)) {
					try {
		
						caozuoyuan caozuoyuan = daofactory.getInstance().getcaozuoyuanDao().login(userName,password);
						if(caozuoyuan !=null) {
							session.setAttribute("user", caozuoyuan);
							session.setAttribute("type", type);
							resp.sendRedirect("index.jsp");
						}else {
							req.setAttribute("error", "账号或密码错误");
							req.getRequestDispatcher("login.jsp").forward(req, resp);
						}
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}else {
					//锟斤拷锟斤拷员
					try {
						guanliyuan guanliyuan = daofactory.getInstance().getguanliyuanDao().login(userName,password);
						if (guanliyuan != null) {
							session.setAttribute("user", guanliyuan);
							session.setAttribute("type", type);
							resp.sendRedirect("index.jsp");
						}else {
							req.setAttribute("error", "账号或密码错误");
							req.getRequestDispatcher("login.jsp").forward(req, resp);
						}
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
					
			}else {
				
			}
		}

}
